﻿Public Class ThanNhan
    Private xltn As XLThanNhan
    Private sta As Boolean = False
    Private data As DataTable
    Private dataTN As DataTable
    Private dstn As New List(Of DTO_ThanNhan.ThanNhan)
    Private dspb As List(Of DTO_PhongBan.PhongBan)
    Dim vt As Integer = 0
    Dim manv As String = ""
    Dim nodeClick As String = ""
    Private Sub tr_dsnv_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tr_dsnv.AfterSelect
        Lv_ThanNhan.Items.Clear()
        Lv_ThanNhan.View = View.Details
        Lv_ThanNhan.GridLines = True
        Lv_ThanNhan.FullRowSelect = True
        xltn = New XLThanNhan
        nodeClick = tr_dsnv.SelectedNode.Text
        Dim i As Integer = 0
        dstn = xltn.BUS_TimThanNhan(nodeClick, data, dataTN) 'tim than nhan trong datatn co manv= data(nhanvien)
        If dstn.Count <> 0 Then
            For Each nv As DTO_ThanNhan.ThanNhan In dstn
                LviewShow(nv)
            Next
        End If
    End Sub

    Private Sub them_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles them.Click
        ThemNode()
        Refesh_()
    End Sub

    Private Sub xoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xoa.Click
        Dim x As TreeNode
        xltn = New XLThanNhan
        x = tr_dsnv.SelectedNode
        Dim tentn As String = tr_dsnv.SelectedNode.Text
        If IsNothing(x) = True Then
            MessageBox.Show("Không có đối tượng xoá", "Thông Báo", MessageBoxButtons.OK)
        Else
            Dim tenNV As String = tr_dsnv.SelectedNode.Text
            Dim mnv As String = xltn.BUS_TimMaNV(tenNV, data)
            For Each dr As DataRow In dataTN.Rows
                If mnv = dr("MaNV") And dr("HoTen") = Lv_ThanNhan.Items(vt).SubItems(2).Text Then
                    Lv_ThanNhan.Items.RemoveAt(vt)
                    For j As Integer = 0 To tr_dsnv.SelectedNode.Nodes.Count - 1
                        If x.Nodes.Item(j).Text = dr("HoTen") Then
                            x.Nodes.Item(j).Remove()
                            Exit For
                        End If
                    Next
                    dr.Delete()
                    Refesh_()
                    Exit For
                End If
            Next
        End If
    End Sub

    Private Sub capnhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles capnhat.Click
        If sta = True Then
            Dim x As TreeNode
            x = tr_dsnv.SelectedNode
            xltn = New XLThanNhan
            Dim tn As New DTO_ThanNhan.ThanNhan
            If LayDLieu(tn) = False Then
                Dim tenNV As String = tr_dsnv.Parent.Text
                For Each dr As DataRow In dataTN.Rows
                    If Lv_ThanNhan.Items(vt).SubItems(1).Text = dr("MaNV") And dr("HoTen") = Lv_ThanNhan.Items(vt).SubItems(2).Text Then
                        xltn.BUS_ThemTN(dr, tn)
                        For i As Integer = 0 To x.Nodes.Count - 1
                            If x.Nodes.Item(i).Text = tn.HoTen Then
                                x.Nodes.Item(i).Text = tn.HoTen
                                x.Nodes.Item(i).Nodes.Item(0).Text = tn.MaNV
                                x.Nodes.Item(i).Nodes.Item(1).Text = tn.NamSinh
                                x.Nodes.Item(i).Nodes.Item(2).Text = tn.GioiTinh
                                x.Nodes.Item(i).Nodes.Item(3).Text = tn.NguyenQuan
                                x.Nodes.Item(i).Nodes.Item(4).Text = tn.QuanHe
                                x.Nodes.Item(i).Nodes.Item(5).Text = tn.NgheNghiep
                                x.Nodes.Item(i).Nodes.Item(6).Text = tn.NoiLam
                                x.Nodes.Item(i).Nodes.Item(7).Text = tn.NoiO
                            End If
                        Next
                        'cap nhant tren listview
                        Lv_ThanNhan.Items(vt).SubItems(1).Text = tn.MaNV
                        Lv_ThanNhan.Items(vt).SubItems(2).Text = tn.HoTen
                        Lv_ThanNhan.Items(vt).SubItems(3).Text = tn.GioiTinh
                        Lv_ThanNhan.Items(vt).SubItems(4).Text = tn.NamSinh
                        Lv_ThanNhan.Items(vt).SubItems(5).Text = tn.NguyenQuan
                        Lv_ThanNhan.Items(vt).SubItems(6).Text = tn.QuanHe
                        Lv_ThanNhan.Items(vt).SubItems(7).Text = tn.NgheNghiep
                        Lv_ThanNhan.Items(vt).SubItems(8).Text = tn.NoiLam
                        Lv_ThanNhan.Items(vt).SubItems(9).Text = tn.NoiO
                    End If
                Next
            End If
            Refesh_()
        End If
        
    End Sub

    Private Sub Lamlai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Lamlai.Click
        Refesh_()
    End Sub

    Private Sub save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save.Click
        xltn = New XLThanNhan
        If xltn.BUS_SaveTN(dataTN) = True Then
            MessageBox.Show("Lưu Thành Công ", "Thông Báo", MessageBoxButtons.OK)
        End If
    End Sub

    Private Sub reload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles reload.Click
        RLoad()
    End Sub

    Private Sub close__Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles close_.Click
        Me.Close()
    End Sub

    Private Sub ThanNhan_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Dim dr As DialogResult = MessageBox.Show("Bạn có muốn lưu dữ liệu lại trước khi đóng màn hình này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If dr = Windows.Forms.DialogResult.Yes Then
            xltn = New XLThanNhan
            If xltn.BUS_SaveTN(dataTN) = True Then
                MessageBox.Show("Lưu Thành Công ", "Thông Báo", MessageBoxButtons.OK)
            End If
        End If
    End Sub

    Private Sub ThanNhan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RLoad()
    End Sub

    Private Sub Lv_ThanNhan_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Lv_ThanNhan.SelectedIndexChanged
        Try
            vt = Lv_ThanNhan.SelectedItems(0).Index
            tb_hoten.Text = Lv_ThanNhan.SelectedItems(0).SubItems(2).Text()
            tb_tuoi.Text = Lv_ThanNhan.SelectedItems(0).SubItems(4).Text()
            If Lv_ThanNhan.SelectedItems(0).SubItems(3).Text() = "Nam" Then
                rd_nam.Checked = True
            Else
                rd_nu.Checked = True
            End If
            tb_nguyenquan.Text = Lv_ThanNhan.SelectedItems(0).SubItems(5).Text()
            tb_quanhe.Text = Lv_ThanNhan.SelectedItems(0).SubItems(6).Text()
            tb_nghenghiep.Text = Lv_ThanNhan.SelectedItems(0).SubItems(7).Text()
            tb_noilam.Text = Lv_ThanNhan.SelectedItems(0).SubItems(8).Text()
            tb_noio.Text = Lv_ThanNhan.SelectedItems(0).SubItems(9).Text()
            sta = True
        Catch ex As Exception
        End Try
    End Sub

#Region "thuc hien"
    Private Sub RLoad()
        xltn = New XLThanNhan
        Dim tree_ As New TreeNode("CTy TNHH Cộng Sự Giải Pháp Thông Tin")
        data = New DataTable
        dataTN = New DataTable
        Dim pb As New XLPhongBan
        Dim nv As New XLNhanVien
        dspb = pb.BUS_LayDSPB()
        data = nv.BUS_LayDSNV()
        dataTN = xltn.BUS_LayDSTN()
        If data.Rows.Count = 0 Then Return
        If dspb.Count < 0 Then
        Else
            For Each d As DTO_PhongBan.PhongBan In dspb
                tree_.Nodes.Add(xltn.BUS_AddTreePB(d.TenPhong, d.MaPB, data, dataTN))
            Next
            tr_dsnv.Nodes.Add(tree_)
            tr_dsnv.ExpandAll()
        End If
    End Sub

    Private Function LayDLieu(ByRef tn As DTO_ThanNhan.ThanNhan) As Boolean
        xltn = New XLThanNhan
        Dim x As String= tr_dsnv.SelectedNode.Text
        tn.MaNV = xltn.BUS_TimMaNV(x, data)
        tn.HoTen = tb_hoten.Text
        tn.GioiTinh = "Nam"
        If rd_nu.Checked = True Then
            tn.GioiTinh = "Nu"
        End If
        tn.NguyenQuan = tb_nguyenquan.Text
        tn.NamSinh = tb_tuoi.Text
        tn.NgheNghiep = tb_nghenghiep.Text
        tn.NoiLam = tb_noilam.Text
        tn.NoiO = tb_noio.Text
        tn.QuanHe = tb_quanhe.Text
        Return xltn.BUS_KTra(tn)
    End Function
    Private Sub Refesh_()
        tb_hoten.Text = String.Empty
        tb_nghenghiep.Text = String.Empty
        tb_nguyenquan.Text = String.Empty
        tb_noilam.Text = String.Empty
        tb_noio.Text = String.Empty
        tb_quanhe.Text = String.Empty
        tb_tuoi.Text = String.Empty
        rd_nam.Checked = True
        'dlieu = String.Empty
        vt = 0
    End Sub
    Private Sub LviewShow(ByVal tn As DTO_ThanNhan.ThanNhan)
        Dim item As New ListViewItem
        item.Text = "STT"
        item.SubItems.Add(tn.MaNV)
        item.SubItems.Add(tn.HoTen)
        item.SubItems.Add(tn.GioiTinh)
        item.SubItems.Add(tn.NamSinh)
        item.SubItems.Add(tn.NguyenQuan)
        item.SubItems.Add(tn.QuanHe)
        item.SubItems.Add(tn.NgheNghiep)
        item.SubItems.Add(tn.NoiLam)
        item.SubItems.Add(tn.NoiO)
        Lv_ThanNhan.Items.Add(item)
        capnhatSTT()
    End Sub
    Private Sub capnhatSTT()
        For i As Integer = 1 To Lv_ThanNhan.Items.Count
            Lv_ThanNhan.Items(i - 1).Text = i.ToString
        Next
    End Sub
    Private Sub ThemNode()
        xltn = New XLThanNhan
        Dim tn As New DTO_ThanNhan.ThanNhan
        Dim x As TreeNode
        x = tr_dsnv.SelectedNode
        If IsNothing(x) = True Then
            MessageBox.Show("Bạn chưa chọn Nhân Viên của mối quan hệ này", "Thông Báo", MessageBoxButtons.OK)
        Else
            If LayDLieu(tn) = False And xltn.BUS_TimMaNV(nodeClick, data) <> "" Then
                Dim dt As DataRow = dataTN.NewRow()
                xltn.BUS_ThemTN(dt, tn)
                dataTN.Rows.Add(dt) 'them vao datarow
                LviewShow(tn) 'show len listview
                x.Nodes.Add(xltn.BUS_AddTreeTN(tn)) 'hien len tree view
                'tr_dsnv.SelectedNode = Nothing
            Else
                MessageBox.Show("Dữ liệu nhập vào sai hoặc thiếu", "Thông Báo", MessageBoxButtons.OK)
            End If
        End If
    End Sub
    
#End Region

End Class
